/*
 * @Descripttion: 
 * @version: 
 * @Author: 宁乃楚
 * @Date: 2021-07-01 15:37:05
 * @LastEditors: 宁乃楚
 * @LastEditTime: 2021-07-05 10:00:18
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
    // 防止路由重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    // 默认加载界面
    {
        path: '/',
        redirect: '/login'
    },
    // 登录页面界面
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('@/views/Login.vue')
    },
    // 注册页面路由
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('@/views/Register.vue')
    },
    // 总路由
    {
        path: '/manager',
        name: 'manager',
        component: () =>
            import ('@/views/Manager.vue'),
        children: [
            // 配置子路由的默认加载页面
            {
                path: '/manager',
                redirect: '/manager/home'
            },
            // 首页路由
            {
                path: 'home',
                component: () =>
                    import ('@/views/Home/Home.vue')
            },
            // 订单路由
            {
                path: 'order',
                component: () =>
                    import ('@/views/Order/Order.vue')
            },
            // 帮助路由
            {
                path: 'help',
                component: () =>
                    import ('@/views/Help/Help.vue')
            },
            // 我的页面路由
            {
                path: 'mine',
                component: () =>
                    import ('@/views/Mine/Mine.vue')
            },
            //产品页面路由
            {
                path: 'product',
                component: () =>
                    import ('@/views/Product/product.vue')
            },
            // 订单详情界面
            {
                path: 'detail',
                component: () =>
                    import ('@/views/Order/orderDetail.vue')
            },
            // 常用地址界面
            {
                path: 'address',
                component: () =>
                    import ('@/views/Mine/address.vue')
            },
              // 账户安全界面
              {
                path:'account',
                component:()=>import('@/views/Mine/account.vue')
            },
            // 用户个人信息界面
            {
                path:'info',
                component:()=>import('@/views/Mine/userInfo.vue')
            },
            //订单提交 页面路由
            {
                path: 'orderConfirm',
                component: () =>import ('@/views/OrderConfirm/OrderConfirm.vue')
            },
            // 实名认证界面
            {
                path:'certification',
                component: () =>import ('@/views/Mine/Certification.vue')
            },
            // 完善用户信息界面
            {
                path:'complete',
                component: () =>import ('@/views/Mine/complete.vue')
            },
        ]
    },

]

const router = new VueRouter({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫 判断有无token
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
    //若要跳转的页面是登录界面
    if (to.path === '/login') {
        //直接跳转
        next();
    } else { //若想要跳转其他页面
        //获取本地存储的token值
        let token = localStorage.getItem('token');
        //若token为空则验证不成功，跳转到登录页面
        if (token === 'null' || token === '') {
            next('/login');
        } else {
            next();
        }
    }
});

export default router